﻿using Booking.Salary;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Booking.Service
{
    public class SalaryItemService
    {

        /*
         * 1.新增
         * 2.修改
         * 3.删除
         * 4.查询
         * 5.设置/取消成常用
         *
         * 6.按名字查询是否存在
         */

        /// <summary>
        /// 新增一个工资项目
        /// </summary>
        /// <param name="item">工资项目</param>
        /// <returns>是否增加成功</returns>
        public async Task<bool> AddAsync(SalaryItem item)
        {
            using (var context = new BookingContext())
            {
                context.SalaryItems.Add(item);
                var res = await context.SaveChangesAsync();
                return res > 0 ? true : false;
            }

        }
        /// <summary>
        /// 更新一个数据
        /// </summary>
        /// <param name="item">新的数据</param>
        /// <returns>是否更新成功</returns>
        public async Task<bool> UpdateAsync(SalaryItem item)
        {
            using (var context = new BookingContext())
            {
                var exitItem = context.SalaryItems.Single(s => s.Id == item.Id);
                exitItem.Name = item.Name;
                exitItem.CommonlyUsed = item.CommonlyUsed;
                exitItem.SalaryType = item.SalaryType;
                exitItem.Validation = item.Validation;
                var res = await context.SaveChangesAsync();
                return res > 0 ? true : false;


            }

        }
        /// <summary>
        /// 删除一个数据
        /// </summary>
        /// <param name="id">要删除的数据的ID</param>
        /// <returns>是否删除成功</returns>
        public async Task<bool> DeleteAsync(int id)
        {

            using (var context = new BookingContext())
            {
                var item = context.SalaryItems.Single(s => s.Id == id);
                item.Validation = false;
                var res = await context.SaveChangesAsync();
                return res > 0 ? true : false;

            }
        }

        /// <summary>
        /// 查询所有的有效数据
        /// </summary>
        /// <returns>数据集合</returns>
        public async Task<List<SalaryItem>> QueryAsync()
        {
            
            using (var context = new BookingContext())
            {
                return await context.SalaryItems.Where(s=>s.Validation).ToListAsync();
            
            }
        }
        /// <summary>
        /// 按名字关键字查询
        /// </summary>
        /// <param name="KeyWord">关键字</param>
        /// <returns></returns>
        public async Task<List<SalaryItem>> QueryAsync(string KeyWord)
        {

            using (var context = new BookingContext())
            {
                return await context.SalaryItems.Where(s => s.Name.Contains(KeyWord)&&s.Validation).ToListAsync();


            }

        }
        /// <summary>
        /// 查询常用的
        /// </summary>
        /// <returns></returns>
        public async Task<List<SalaryItem>> QueryCommonAsync()
        {
            using (var context = new BookingContext())
            {
                return await context.SalaryItems.Where(s => s.Validation && s.CommonlyUsed).ToListAsync();
            }

        }

        /// <summary>
        /// 按类型查询
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public async Task<List<SalaryItem>> QueryAsync(SalaryType type)
        {

            using (var context = new BookingContext())
            {
                return await context.SalaryItems.Where(s => s.SalaryType==type && s.Validation).ToListAsync();


            }

        }
        /// <summary>
        /// 设置成常用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> SetCommonlyUseAsync(int id)
        {
            using (var context = new BookingContext())
            {
                var item = context.SalaryItems.Single(s => s.Id == id);
                item.CommonlyUsed = !item.CommonlyUsed;
                var res = await context.SaveChangesAsync();
                return res > 0 ? true : false;

            }

        }

        /// <summary>
        /// 查询名字是否存在
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public async Task<bool> QueryNameExist(string Name)

        {
            using (var context = new BookingContext())
            {
              return await  context.SalaryItems.AnyAsync(s => s.Name == Name);

            }

        }

    }

}
